<?php
if (!defined('ABSPATH')) {
	define( 'ABSPATH', dirname(dirname(__FILE__)) . '/' );
}

require_once ABSPATH . 'dr-config.php';
require_once ABSPATH . DRINC . '/dr-base.php';
require_once ABSPATH . DRINC . '/dr-functions.php';

require_once ABSPATH . DRINC . '/dr-smarty.php';

require_once ABSPATH . DRINC . '/dr-page-check-login.php';

# -------------------------------- #
# manage request
# -------------------------------- #
if (isset($_POST['submit'])) {
	$action = isset($_POST['action']) ? trim($_POST['action']) : '';
	switch ($action) {
		case 'add':
			$us_title = isset($_POST['title']) ? trim($_POST['title']) : '';
			$us_position = isset($_POST['position']) ? intval($_POST['position']) : 0;
			$us_content = isset($_POST['content']) ? trim($_POST['content']) : '';
			
			# XXX 处理输入
			$page = array();
			$page['title']    = $us_title;
			$page['position'] = $us_position;
			$page['content']  = $us_content;
			
			$result = dr_add_page($page);
			header('Location: dr-page-admin-pages.php');
			break;
			
		case 'edit':
			$us_page_id = isset($_POST['page_id']) ? intval($_POST['page_id']) : 0;
			$us_title = isset($_POST['title']) ? trim($_POST['title']) : '';
			$us_position = isset($_POST['position']) ? intval($_POST['position']) : 0;
			$us_content = isset($_POST['content']) ? trim($_POST['content']) : '';
			
			# XXX 处理输入
			$page = array();
			$page['page_id'] = $us_page_id;
			$page['title']    = $us_title;
			$page['position'] = $us_position;
			$page['content']  = $us_content;
			
			$result = dr_update_page($page);
			header('Location: dr-page-admin-pages.php');
			break;
			
		default:
			die('An error occur when manage request of "Pages"');
	}
}


# 获得将要显示的页面
$fragment = isset($_GET['fragment']) ? intval($_GET['fragment']) : 0;
$smarty->assign('fragment', $fragment);
# -------------------------------- #
# show page
# -------------------------------- #
switch ($fragment) {
	case 0: # 显示所有的页面的列表
		$smarty->assign('page_title', '页面 - iDaren');
		$smarty->assign('pages', dr_get_all_pages_without_content());
		break;
	case 1: # 添加新的页面
		$smarty->assign('page_title', '添加页面 - iDaren');
		break;
	case 2: # 编辑页面
		$smarty->assign('page_title', '编辑页面 - iDaren');
		$page_id = isset($_GET['pid']) ? intval($_GET['pid']) : 0;
		if ($page_id > 0) {
			$page = dr_get_page_with_id($page_id);
			if ($page) {
				$smarty->assign('page', $page);
			} else {
				die('Cannot find page with given page id');
			}
		} else {
			die('An error occurs when edit page with invalid page id.');
		}
		break;
	default:
		die('An error occur when manage "Pages" of iDaren');
}

$smarty->display('admin/admin-pages.tpl');

# ================================== #
# functions
# ================================== #

function dr_get_all_pages_without_content() {
	$posts = array(
		'action' => 'get_all_pages_without_content',
	);
	$result = dr_server_request('dr-app-pages.php', $posts);
	return json_decode($result, true);
}

//var_dump(dr_get_all_pages_without_content());

function dr_get_page_with_id($page_id) {
	$posts = array(
		'action' => 'get_page_with_id',
		'page_id' => $page_id
	);
	$result = dr_server_request('dr-app-pages.php', $posts);
	return json_decode($result, true);
}

function dr_add_page($page) {
	$posts = array(
		'action' => 'add_page',
		'page'   => json_encode($page)
	);
	
	$json_result = dr_server_request('dr-app-pages.php', $posts);
	$array_result = json_decode($json_result, true);
	
	if ($array_result['ret'] === 1) {
		return true;
	}
	return false;
}

function dr_update_page($page) {
	$posts = array(
		'action' => 'update_page',
		'page'   => json_encode($page)
	);
	
	$json_result = dr_server_request('dr-app-pages.php', $posts);
	$array_result = json_decode($json_result, true);
	
	if ($array_result['ret'] === 1) {
		return true;
	}
	return false;
}